<template>
  <div class="index_video" style="padding-left:15px">
    <h3>首页视频上传编辑</h3>
    <input type="button" class="btn btn-info" value="新增视频" @click="add_video()">
    <div class="row" style="margin-top:20px">
      <div class="row_height">
        <div class="col-md-12" v-for="(item,index) in main_list" :key="index" style="margin:50px 0">
          <div class="img_box">
            <img :src="item.video_img" alt="" class="full-width">
          </div>
          <h5>中文标题：{{ item.video_tle }}</h5>
          <h5>英文标题：{{ item.video_tleen }}</h5>
          <div class="btn_box">
            <!-- <input type="button" value="编辑" @click="changeData(item)" class="btn btn-info"> -->
            <input type="button" value="删除" @click="deleteData(item)" class="btn btn-danger">
            <input type="button" value="编辑" @click="editData(item)" class="btn btn-info">
          </div>
        </div>
      </div>
    </div>
    <!-- 新增视频弹窗 -->
    <el-dialog title="新增视频弹窗" :visible.sync="dialog_show">
      <el-form enctype="multipart/form-data" id="uploadForm">
        <el-form-item label="视频标题（中文）" label-width="140px">
          <el-input auto-complete="off" v-model="video.tle" placeholder="输入视频标题"></el-input>
        </el-form-item>
        <el-form-item label="视频标题（英文）" label-width="140px">
          <el-input auto-complete="off" v-model="video.tleen" placeholder="输入视频标题（英文）"></el-input>
        </el-form-item>
        <el-form-item label="选择视频封面图片" label-width="140px">
          <input type="file" name="video_img" id="video_img">
        </el-form-item>
        <el-form-item label="选择视频文件" label-width="140px">
          <input type="file" name="video_file" id="video_file">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_show = false">取 消</el-button>
        <el-button type="primary" @click="add_new()">确定</el-button>
      </div>
    </el-dialog>
    <!-- 新增视频弹窗  end -->

  </div>
</template>
<script>
import g from '../../components/global.js'
export default {
  data() {
    return {
      dialog_show: false,
      dialog_tle: '新增视频弹窗',
      video: {
        tle: '',
        tleen: '',
        id: ''
      },
      edit_video: {},
      main_list: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    add_video() {
      this.dialog_show = true
      clear()
    },
    add_new() {
      var that = this
      var formData = new FormData()
      if (that.video.id == '') {
        formData.append('video_img', $('#video_img')[0].files[0])
        formData.append('video_file', $('#video_file')[0].files[0])
      } else {
        formData.append('video_id', that.video.id)
        if (!$('#video_img').val() == '') {
          formData.append('video_img', $('#video_img')[0].files[0])
        }
        if (!$('#video_file').val() == '') {
          formData.append('video_file', $('#video_file')[0].files[0])
        }
      }
      formData.append('video_tle', that.video.tle)
      formData.append('video_tleen', that.video.tleen)
      $.ajax({
        url: g.res_url + '/api/v1/sendData',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
          if (data == 'success') {
            that.$message.success('操作成功！')
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(returndata) {
          that.$message.error('网络错误！')
        }
      })
      that.video.id = ''
      that.dialog_show = false
    },
    getData() {
      var that = this
      $.ajax({
        url: g.res_url + '/api/v1/getvideo',
        type: 'post',
        success: function(data) {
          var dataObj = JSON.parse(data)
          that.main_list = dataObj
     
        },
        error: function() {
          that.$message.error('网络繁忙！')
        }
      })
    },
    deleteData(row) {
      var that = this
      $.ajax({
        url: g.res_url + '/api/v1/delete_video',
        type: 'post',
        data: {
          del_id: row.id
        },
        success: function(data) {
          if (data == 'success') {
            that.$message.success('操作成功！')
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function() {
          that.$message.error('网络繁忙！')
        }
      })
    },
    editData(row) {
      var _this = this
      clear()
      _this.dialog_show = true
      _this.dialog_tle = '修改信息'
      _this.video.tle = row.video_tle
      _this.video.tleen = row.video_tleen
      _this.video.id = row.id
    }
  }
}
function clear() {
  $('#video_file').val('')
  $('#video_img').val('')
}
</script>
